package Main;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;

import air_traffic_control.Airport;
import air_traffic_control.Plane;
import air_traffic_control.UnregisteredPlaneException;

public class Main {
	private static final String IS_REDIRECTED_MESSAGE = " is redirected !";
	private static final String IS_CLOSED_FOR_MESSAGE = " is closed for ";
	private static final String TAKING_OFF = "takingoff";
	private static final String LANDING = "landing";
	private static final String LANDING_MESSAGE = " will land on track ";

	public static void main(String[] args) throws IOException,
			UnregisteredPlaneException {
		BufferedReader reader = new BufferedReader(new FileReader(
				"InputFile.txt"));
		Writer writer = null;
		writer = new BufferedWriter(new OutputStreamWriter(
				new FileOutputStream("output.txt")));

		try {
			String line = reader.readLine();
			String[] parts = line.split(" ");

			// System.out.println(Arrays.toString(parts));

			int numberOfTracks = Integer.parseInt(parts[0]);
			int maxCapacity = Integer.parseInt(parts[1]);
			int timeToLandOrTakeoff = Integer.parseInt(parts[2]);

			Airport sofia = new Airport(numberOfTracks, maxCapacity,
					timeToLandOrTakeoff);

			while (line != null) {

				String inputData = reader.readLine();

				if (inputData == null)
					break;

				String[] planeData = inputData.split(" ");

				String planeOrTrackName = planeData[0];
				String landsOrTakeoff = planeData[1];

				if (landsOrTakeoff.equals(LANDING)) {

					// you will have 3 elements in planeData only if the plane
					// is landing
					int fuel = Integer.parseInt(planeData[2]);

					Plane currentPlane = new Plane(planeOrTrackName, fuel);

					int numberOfTrack = sofia.requestToLand(currentPlane);

					if (numberOfTrack != Airport.NO_AVAILABLE_TRACK) {
						writer.write(currentPlane.getName() + LANDING_MESSAGE
								+ "on track " + (numberOfTrack + 1)+ " in "
								+ sofia.trackActionWillBeDoneIn(numberOfTrack)
								+ "min" + "\n");
					} else {
						writer.write("Plane " + currentPlane.getName()
								+ IS_REDIRECTED_MESSAGE + "\n");
					}

				} else if (landsOrTakeoff.equals(TAKING_OFF)) {

					int trackNumber = sofia.requestToTakeOff(planeOrTrackName);

					writer.write(planeOrTrackName
							+ " will take off from track " + (trackNumber + 1)
							+ " in "
							+ sofia.trackActionWillBeDoneIn(trackNumber) + "min" + "\n");
				} else {
					int time = Integer.parseInt(planeData[2]);

					writer.write(planeOrTrackName + IS_CLOSED_FOR_MESSAGE
							+ time + "min" + "\n");

					sofia.closeTrack(Integer.parseInt(planeOrTrackName), time);

				}

				sofia.onTimerTick();
			}

		} catch (UnregisteredPlaneException e) {
			e.printStackTrace();
		} finally {
			writer.close();
			reader.close();
		}

	}
}
